//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.gui.fixed;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
import org.jpgolay.gui.date.DateComboBox;

public class ReferenceViewer extends JPanel {

    public ReferenceViewer() {
        dateJLabel = null;
        salespersonJLabel = null;
        pieceComboBox = null;
        idTextField = null;
        dateComboBox = null;
        salespersonTextField = null;
        selectedStringPiece = "";
        selectedIndexPiece = -1;
        idValue = "N/A";
        salespersonValue = "N/A";
        date = "";
        initialize();
    }

    private void initialize() {
        setLayout(null);
        setSize(300, 83);
        add(getDateJLabel(), null);
        add(getSalespersonJLabel(), null);
        add(getPieceJComboBox(), null);
        add(getIdJTextField(), null);
        add(getDateJComboBox(), null);
        add(getSalespersonJTextField(), null);
    }

    private JLabel getDateJLabel() {
        if (dateJLabel == null) {
            dateJLabel = new JLabel();
            dateJLabel.setText("Date");
            dateJLabel.setSize(100, 23);
            dateJLabel.setLocation(5, 30);
        }
        return dateJLabel;
    }

    private JLabel getSalespersonJLabel() {
        if (salespersonJLabel == null) {
            salespersonJLabel = new JLabel();
            salespersonJLabel.setText("Salesperson");
            salespersonJLabel.setSize(100, 23);
            salespersonJLabel.setLocation(5, 55);
        }
        return salespersonJLabel;
    }

    private JComboBox getPieceJComboBox() {
        if (pieceComboBox == null) {
            pieceComboBox = new JComboBox();
            pieceComboBox.setModel(new DefaultComboBoxModel(listPiece));
            pieceComboBox.setLocation(5, 5);
            pieceComboBox.setSize(200, 23);
            pieceComboBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
            pieceComboBox.setFont(new Font("MS Sans Serif", 1, 12));
            pieceComboBox.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    String oldString = selectedStringPiece;
                    int oldIndex = selectedIndexPiece;
                    selectedIndexPiece = pieceComboBox.getSelectedIndex();
                    selectedStringPiece = (String) pieceComboBox.getSelectedItem();
                    firePropertyChange("selectedIndexPiece", oldIndex, selectedIndexPiece);
                    firePropertyChange("selectedStringPiece", oldString, selectedStringPiece);
                }
            });
        }
        return pieceComboBox;
    }

    private JTextField getIdJTextField() {
        if (idTextField == null) {
            idTextField = new JTextField();
            idTextField.setText(idValue);
            idTextField.setSize(85, 23);
            idTextField.setLocation(210, 5);
            idTextField.setHorizontalAlignment(0);
            idTextField.setEnabled(true);
            idTextField.setEditable(false);
            idTextField.addKeyListener(new KeyAdapter() {
                public void keyReleased(KeyEvent e) {
                    setIdValue(idTextField.getText());
                }
            });
        }
        return idTextField;
    }

    private DateComboBox getDateJComboBox() {
        if (dateComboBox == null) {
            dateComboBox = new DateComboBox();
            dateComboBox.setSize(185, 23);
            dateComboBox.setLocation(110, 30);
            dateComboBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
            dateComboBox.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    String old = date;
                    date = (String) dateComboBox.getSelectedItem();
                    firePropertyChange("date", old, date);
                }
            });
        }
        return dateComboBox;
    }

    private JTextField getSalespersonJTextField() {
        if (salespersonTextField == null) {
            salespersonTextField = new JTextField();
            salespersonTextField.setText(salespersonValue);
            salespersonTextField.setSize(185, 23);
            salespersonTextField.setLocation(110, 55);
            salespersonTextField.setEditable(false);
            salespersonTextField.addKeyListener(new KeyAdapter() {
                public void keyReleased(KeyEvent e) {
                    setSalespersonValue(salespersonTextField.getText());
                }
            });
        }
        return salespersonTextField;
    }

    public void setDateLabel(String txt) {
        getDateJLabel().setText(txt);
    }

    public String getDateLabel() {
        return getDateJLabel().getText();
    }

    public void setSalespersonLabel(String txt) {
        getSalespersonJLabel().setText(txt);
    }

    public String getSalespersonLabel() {
        return getSalespersonJLabel().getText();
    }

    public String[] getListPiece() {
        return listPiece;
    }

    public void setListPiece(String listPiece[]) {
        String old[] = listPiece;
        this.listPiece = listPiece;
        firePropertyChange("listPiece", old, listPiece);
        pieceComboBox.setModel(new DefaultComboBoxModel(listPiece));
    }

    public int getSelectedIndexPiece() {
        return selectedIndexPiece;
    }

    public void setSelectedIndexPiece(int selectedIndexPiece) {
        int old = selectedIndexPiece;
        this.selectedIndexPiece = selectedIndexPiece;
        firePropertyChange("selectedIndexPiece", old, selectedIndexPiece);
        pieceComboBox.setSelectedIndex(selectedIndexPiece);
    }

    public String getSelectedStringPiece() {
        return selectedStringPiece;
    }

    public void setSelectedStringPiece(String selectedStringPiece) {
        String old = selectedStringPiece;
        this.selectedStringPiece = selectedStringPiece;
        firePropertyChange("selectedStringPiece", old, selectedStringPiece);
        pieceComboBox.setSelectedItem(selectedStringPiece);
    }

    public String getIdValue() {
        return idValue;
    }

    public void setIdValue(String idValue) {
        String old = idValue;
        this.idValue = idValue;
        firePropertyChange("idValue", old, idValue);
        getIdJTextField().setText(idValue);
    }

    public String getSalespersonValue() {
        return salespersonValue;
    }

    public void setSalespersonValue(String val) {
        String old = val;
        salespersonValue = val;
        firePropertyChange("val", old, val);
        getSalespersonJTextField().setText(val);
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        String old = date;
        this.date = date;
        firePropertyChange("date", old, date);
        dateComboBox.setModel(new DefaultComboBoxModel(new String[]{
                    date
                }));
    }

    public JTextField getSalespersonTextField() {
        return getSalespersonJTextField();
    }

    public JTextField getIdTextField() {
        return getIdJTextField();
    }

    public void clean() {
        setDate("");
        setSalespersonValue("");
        setIdValue("");
    }

    public void setEnabledDateAndPiece(boolean b) {
        getDateJComboBox().setEnabled(b);
        getPieceJComboBox().setEnabled(b);
    }
    private JLabel dateJLabel;
    private JLabel salespersonJLabel;
    private JComboBox pieceComboBox;
    private JTextField idTextField;
    private DateComboBox dateComboBox;
    private JTextField salespersonTextField;
    private String listPiece[] = {
        "N/A"
    };
    private String selectedStringPiece;
    private int selectedIndexPiece;
    private String idValue;
    private String salespersonValue;
    private String date;
}
